﻿using Command.CommunityToolkit.Attributes;
using Command.CommunityToolkit.Services;
using ShellProgressBar;

namespace Command.CommunityToolkit.Commands;

public abstract class CommandBase:ICommand
{

    public readonly IQualityControl QualityControl;
    public readonly ProgressBarOptions Options;
    public object Arg;
    public CommandBase(object arg, IQualityControl qualityControl)
    {
        Arg = arg;
        QualityControl = qualityControl;
        Options = new ProgressBarOptions
        {
            ForegroundColor = ConsoleColor.Yellow,
            ForegroundColorDone = ConsoleColor.DarkGreen,
            BackgroundColor = ConsoleColor.DarkGray,
            BackgroundCharacter = '\u2593'
        };
    }
    public abstract string Summary();
    public abstract string Description();
    public abstract string[] GetLicenses();
    [Log("结果输出")]
    public void ConsoleResult(string msg)
    {
        Console.WriteLine($"\n\n\n{msg}");
    }

    public virtual bool Excute()
    {
        try
        {
            var files = GetLicenses();
            if (!files.Any()) throw new InvalidDataException("没有找到匹配证书压缩文件");
            var ok = false;
            using (var bar = new ProgressBar(files.Length, "Initial message", Options))
            {
                for (int i = 0; i < files.Length; i++)
                {
                    var zipPath = files[i];
                    bar.Tick(i + 1, $@"验证文件：{zipPath}");
                    if (!ok) ok = ok || QualityControl.Execute(zipPath);
                }
            }
            ConsoleResult(ok ? "验证：通过" : "验证：不通过");
            return ok;
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            return false;
        }
           
    }

        
}